package com.xwj.dao;

import com.xwj.domain.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @author xwj
 * @date 2020/11/10
 */
@Mapper
@Repository
public interface OrderMapper {
    /**
     * 新建订单
     *
     * @param order
     * @return
     */
    @Insert("insert into t_order(user_id,product_id,count,money,status) values(#{userId},#{productId},#{count},#{money},0)")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int create(Order order);

    /**
     * 修改订单状态
     *
     * @param orderId
     * @param status
     */
    @Update("update t_order set status = #{status} where id=#{orderId}")
    void update(@Param("orderId") Long orderId, @Param("status") Integer status);

    /**
     * 根据用户id和商品id删除订单
     *
     * @param userId
     * @param productId
     * @return
     */
    @Delete("delete from t_order where user_id=#{userId} and product_id=#{productId}")
    int delete(@Param("userId") Long userId, @Param("productId") Long productId);

    /**
     * 根据id查询订单
     *
     * @param orderId
     * @return
     */
    @Select("select id,user_id as userId,product_id as productId,count,money,status from t_order where id = #{orderId}")
    Order findById(Long orderId);

    /**
     * 修改订单状态
     *
     * @param orderId      订单id
     * @param beforeStatus 预期订单状态
     * @param afterStatus  要修改的订单状态
     * @return
     */
    @Update("update t_order set status = #{afterStatus} where id=#{orderId} and status=#{beforeStatus}")
    int updateStatus(@Param("orderId") Long orderId, @Param("beforeStatus") Integer beforeStatus, @Param("afterStatus") Integer afterStatus);
}
